<template>
  <div class="balance-sheet-table">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="category" label="项目" width="200" />
      <el-table-column prop="item" label="明细" width="200" />
      <el-table-column prop="amount" label="金额(元)" align="right">
        <template #default="scope">
          {{ formatCurrency(scope.row.amount) }}
        </template>
      </el-table-column>
      <el-table-column prop="percentage" label="占比" align="right" width="100">
        <template #default="scope">
          {{ scope.row.percentage }}%
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { computed } from 'vue'

export default {
  name: 'BalanceSheetTable',
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  },
  setup(props) {
    const formatCurrency = (value) => {
      return new Intl.NumberFormat('zh-CN', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      }).format(value || 0)
    }
    
    const tableData = computed(() => {
      if (!props.data || Object.keys(props.data).length === 0) {
        return []
      }
      
      const totalAssets = props.data.totalAssets || 0
      const totalLiabilities = props.data.totalLiabilities || 0
      
      return [
        // 资产类
        { category: '资产', item: '流动资产', amount: props.data.currentAssets, percentage: totalAssets > 0 ? ((props.data.currentAssets / totalAssets) * 100).toFixed(1) : '0.0' },
        { category: '', item: '固定资产', amount: props.data.fixedAssets, percentage: totalAssets > 0 ? ((props.data.fixedAssets / totalAssets) * 100).toFixed(1) : '0.0' },
        { category: '', item: '无形资产', amount: props.data.intangibleAssets, percentage: totalAssets > 0 ? ((props.data.intangibleAssets / totalAssets) * 100).toFixed(1) : '0.0' },
        { category: '', item: '资产总计', amount: totalAssets, percentage: '100.0' },
        
        // 负债类
        { category: '负债', item: '学习债务', amount: props.data.studyDebts, percentage: totalLiabilities > 0 ? ((props.data.studyDebts / totalLiabilities) * 100).toFixed(1) : '0.0' },
        { category: '', item: '时间负债', amount: props.data.timeLiabilities, percentage: totalLiabilities > 0 ? ((props.data.timeLiabilities / totalLiabilities) * 100).toFixed(1) : '0.0' },
        { category: '', item: '负债总计', amount: totalLiabilities, percentage: '100.0' },
        
        // 所有者权益
        { category: '所有者权益', item: '知识资本', amount: props.data.knowledgeCapital, percentage: '' },
        { category: '', item: '留存收益', amount: props.data.retainedEarnings, percentage: '' },
        { category: '', item: '权益总计', amount: props.data.ownerEquity, percentage: '' }
      ]
    })
    
    return {
      tableData,
      formatCurrency
    }
  }
}
</script>

<style scoped>
.balance-sheet-table {
  margin: 20px 0;
}
</style>




